package com.har.modules.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.har.common.exception.RRException;
import com.har.common.utils.StringUtil;
import com.har.common.utils.ValidatUtil;
import com.har.modules.web.dao.UserRegisterDao;
import com.har.modules.web.entity.UserEntity;
import com.har.modules.web.entity.UserRegisterEntity;
import com.har.modules.web.service.SmsService;
import com.har.modules.web.service.UserRegisterService;
import com.har.modules.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service("userRegisterService")
public class UserRegisterServiceImpl extends ServiceImpl<UserRegisterDao,UserRegisterEntity> implements UserRegisterService {
    @Autowired
    private SmsService smsService;

    @Autowired
    private UserService userService;

    @Override
    public UserEntity save(UserRegisterEntity userRegisterEntity) throws ParseException {
        UserEntity user= new UserEntity();
        EntityWrapper<UserEntity> ew=new EntityWrapper<>();
        //验证用户名
        if(StringUtil.strLength(userRegisterEntity.getUserName())>20){
            throw new RRException("请设置以英文字符开头，4-20位包含英文字符或者数字的用户名。");
        }
        if(StringUtil.strLength(userRegisterEntity.getUserName())<4){
            throw new RRException("请设置以英文字符开头，4-20位包含英文字符或者数字的用户名。");
        }
        String str=userRegisterEntity.getUserName().substring(0,1);
        String regex="^[A-Za-z]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        // 字符串是否与正则表达式相匹配
        boolean rs = matcher.matches();
        System.out.println(rs);
        if(!matcher.matches()){
            throw new RRException("请设置以英文字符开头，4-20位包含英文字符或者数字的用户名。");
        }
        //验证用户名是否被使用
        if(!StringUtil.isEmpty(userRegisterEntity.getUserName())){
            ew.eq("user_name",userRegisterEntity.getUserName());
            UserEntity  user1=userService.selectOne(ew);
            //查询
            if(null!=user1){
                throw new RRException("用户名已被注册");
            }
        }
        //验证身份证号
        if(StringUtil.isEmpty(userRegisterEntity.getIdCard())){
            throw new RRException("身份证号不能为空");
        }
        //验证手机号
        if(!ValidatUtil.checkMobileNumber(userRegisterEntity.getMobile())){
            throw  new RRException("请输入正确的手机号");
        }
        //通过验证码和手机号进行验证
        Boolean b=smsService.contrastDate(userRegisterEntity.getMobile(),userRegisterEntity.getCode());
        if (!b){
            throw new RRException("手机验证码失效，请重新获取验证码");
        }
        if(!StringUtil.isEmpty(userRegisterEntity.getCompanyName())){
            user.setCompanyName(userRegisterEntity.getCompanyName());
        }
        if(!StringUtil.isEmpty(userRegisterEntity.getPassword())){
            //md5前端已加密密码
            user.setPassword((userRegisterEntity.getPassword()));
        }
        user.setIdCard(userRegisterEntity.getIdCard());
        user.setMobile(userRegisterEntity.getMobile());
        user.setRealName(userRegisterEntity.getRealName());
        user.setUserName(userRegisterEntity.getUserName());
        //注册成功就正常登录
        user.setStatus(1);
        user.setCreateTime(new Date());
        userService.insert(user);
        return user;
    }
}
